home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / perl5 / Net / DBus / Test / MockMessage.pm < prev    next >
Encoding:
Perl POD Document  |  2008-02-20  |  10.1 KB  |  445 lines

  1. # -*- perl -*-
  2. #
  3. # Copyright (C) 2006 Daniel P. Berrange
  4. #
  5. # This program is free software; You can redistribute it and/or modify
  6. # it under the same terms as Perl itself. Either:
  7. #
  8. # a) the GNU General Public License as published by the Free
  9. #   Software Foundation; either version 2, or (at your option) any
  10. #   later version,
  11. #
  12. # or
  13. #
  14. # b) the "Artistic License"
  15. #
  16. # The file "COPYING" distributed along with this file provides full
  17. # details of the terms and conditions of the two licenses.
  18.  
  19. =pod
  20.  
  21. =head1 NAME
  22.  
  23. Net::DBus::Test::MockMessage - Fake a message object when unit testing
  24.  
  25. =head1 SYNOPSIS
  26.  
  27. Sending a message
  28.  
  29.   my $msg = new Net::DBus::Test::MockMessage;
  30.   my $iterator = $msg->iterator;
  31.  
  32.   $iterator->append_byte(132);
  33.   $iterator->append_int32(14241);
  34.  
  35.   $connection->send($msg);
  36.  
  37. =head1 DESCRIPTION
  38.  
  39. This module provides a "mock" counterpart to the L<Net::DBus::Binding::Message>
  40. class. It is basically a pure Perl fake message object providing the same
  41. contract as the real message object. It is intended for use internally by the
  42. testing APIs.
  43.  
  44. =head1 METHODS
  45.  
  46. =over 4
  47.  
  48. =cut
  49.  
  50. package Net::DBus::Test::MockMessage;
  51.  
  52. use 5.006;
  53. use strict;
  54. use warnings;
  55.  
  56. use vars qw($SERIAL);
  57.  
  58. BEGIN {
  59.     $SERIAL = 1;
  60. }
  61.  
  62. use Net::DBus::Binding::Message;
  63. use Net::DBus::Test::MockIterator;
  64.  
  65. =item my $call = Net::DBus::Test::MockMessage->new_method_call(
  66.   service_name => $service, object_path => $object,
  67.   interface => $interface, method_name => $name);
  68.  
  69. Create a message representing a call on the object located at
  70. the path C<object_path> within the client owning the well-known
  71. name given by C<service_name>. The method to be invoked has
  72. the name C<method_name> within the interface specified by the
  73. C<interface> parameter.
  74.  
  75. =cut
  76.  
  77. sub new_method_call {
  78.     my $proto = shift;
  79.     my $class = ref($proto) || $proto;
  80.     my $self = $class->_new(type => &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_CALL, @_);
  81.  
  82.     bless $self, $class;
  83.  
  84.     return $self;
  85. }
  86.  
  87. =item my $msg = Net::DBus::Test::MockMessage->new_method_return(
  88.     replyto => $method_call);
  89.  
  90. Create a message representing a reply to the method call passed in
  91. the C<replyto> parameter.
  92.  
  93. =cut
  94.  
  95. sub new_method_return {
  96.     my $proto = shift;
  97.     my $class = ref($proto) || $proto;
  98.     my $self = $class->_new(type => &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN, @_);
  99.  
  100.     bless $self, $class;
  101.  
  102.     return $self;
  103. }
  104.  
  105. =item my $signal = Net::DBus::Test::MockMessage->new_signal(
  106.       object_path => $path, interface => $interface, signal_name => $name);
  107.  
  108. Creates a new message, representing a signal [to be] emitted by
  109. the object located under the path given by the C<object_path>
  110. parameter. The name of the signal is given by the C<signal_name>
  111. parameter, and is scoped to the interface given by the
  112. C<interface> parameter.
  113.  
  114. =cut
  115.  
  116. sub new_signal {
  117.     my $proto = shift;
  118.     my $class = ref($proto) || $proto;
  119.     my $self = $class->_new(type => &Net::DBus::Binding::Message::MESSAGE_TYPE_SIGNAL, @_);
  120.  
  121.     bless $self, $class;
  122.  
  123.     return $self;
  124. }
  125.  
  126. =item my $msg = Net::DBus::Test::MockMessage->new_error(
  127.       replyto => $method_call, name => $name, description => $description);
  128.  
  129. Creates a new message, representing an error which occurred during
  130. the handling of the method call object passed in as the C<replyto>
  131. parameter. The C<name> parameter is the formal name of the error
  132. condition, while the C<description> is a short piece of text giving
  133. more specific information on the error.
  134.  
  135. =cut
  136.  
  137. sub new_error {
  138.     my $proto = shift;
  139.     my $class = ref($proto) || $proto;
  140.     my $self = $class->_new(type => &Net::DBus::Binding::Message::MESSAGE_TYPE_ERROR, @_);
  141.  
  142.     bless $self, $class;
  143.  
  144.     return $self;
  145. }
  146.  
  147. sub _new {
  148.     my $proto = shift;
  149.     my $class = ref($proto) || $proto;
  150.     my %params = @_;
  151.     my $self = {};
  152.  
  153.     $self->{type} = exists $params{type} ? $params{type} : die "type parameter is required";
  154.     $self->{interface} = exists $params{interface} ? $params{interface} : undef;
  155.     $self->{path} = exists $params{path} ? $params{path} : undef;
  156.     $self->{destination} = exists $params{destination} ? $params{destination} : undef;
  157.     $self->{sender} = exists $params{sender} ? $params{sender} : undef;
  158.     $self->{member} = exists $params{member} ? $params{member} : undef;
  159.     $self->{error_name} = exists $params{error_name} ? $params{error_name} : undef;
  160.     $self->{data} = [];
  161.     $self->{no_reply} = 0;
  162.     $self->{serial} = $SERIAL++;
  163.     $self->{replyserial} = exists $params{replyto} ? $params{replyto}->get_serial : 0;
  164.  
  165.     bless $self, $class;
  166.  
  167.     if ($self->{type} == &Net::DBus::Binding::Message::MESSAGE_TYPE_ERROR) {
  168.     my $desc = exists $params{error_description} ? $params{error_description} : "";
  169.     my $iter = $self->iterator(1);
  170.     $iter->append_string($desc);
  171.     }
  172.  
  173.     return $self;
  174. }
  175.  
  176.  
  177. =item my $type = $msg->get_type
  178.  
  179. Retrieves the type code for this message. The returned value corresponds
  180. to one of the four C<Net::DBus::Test::MockMessage::MESSAGE_TYPE_*> constants.
  181.  
  182. =cut
  183.  
  184. sub get_type {
  185.     my $self = shift;
  186.  
  187.     return $self->{type};
  188. }
  189.  
  190. =item my $name = $msg->get_error_name
  191.  
  192. Returns the formal name of the error, as previously passed in via
  193. the C<name> parameter in the constructor.
  194.  
  195. =cut
  196.  
  197. sub get_error_name {
  198.     my $self = shift;
  199.     return $self->{error_name};
  200. }
  201.  
  202. =item my $interface = $msg->get_interface
  203.  
  204. Retrieves the name of the interface targetted by this message, possibly
  205. an empty string if there is no applicable interface for this message.
  206.  
  207. =cut
  208.  
  209. sub get_interface {
  210.     my $self = shift;
  211.     
  212.     return $self->{interface};
  213. }
  214.  
  215. =item my $path = $msg->get_path
  216.  
  217. Retrieves the object path associated with the message, possibly an
  218. empty string if there is no applicable object for this message.
  219.  
  220. =cut
  221.  
  222. sub get_path {
  223.     my $self = shift;
  224.     
  225.     return $self->{path};
  226. }
  227.  
  228. =item my $name = $msg->get_destination
  229.  
  230. Retrieves the uniqe or well-known bus name for client intended to be
  231. the recipient of the message. Possibly returns an empty string if
  232. the message is being broadcast to all clients.
  233.  
  234. =cut
  235.  
  236. sub get_destination {
  237.     my $self = shift;
  238.     
  239.     return $self->{destination};
  240. }
  241.  
  242. =item my $name = $msg->get_sender
  243.  
  244. Retireves the unique name of the client sending the message
  245.  
  246. =cut
  247.  
  248. sub get_sender {
  249.     my $self = shift;
  250.     
  251.     return $self->{sender};
  252. }
  253.  
  254. =item my $serial = $msg->get_serial
  255.  
  256. Retrieves the unique serial number of this message. The number
  257. is guarenteed unique for as long as the connection over which
  258. the message was sent remains open. May return zero, if the message
  259. is yet to be sent.
  260.  
  261. =cut
  262.  
  263. sub get_serial {
  264.     my $self = shift;
  265.     
  266.     return $self->{serial};
  267. }
  268.  
  269. =item my $name = $msg->get_member
  270.  
  271. For method calls, retrieves the name of the method to be invoked,
  272. while for signals, retrieves the name of the signal.
  273.  
  274. =cut
  275.  
  276. sub get_member {
  277.     my $self = shift;
  278.     
  279.     return $self->{member};
  280. }
  281.  
  282.  
  283. =item $msg->set_sender($name)
  284.  
  285. Set the name of the client sending the message. The name must
  286. be the unique name of the client.
  287.  
  288. =cut
  289.  
  290. sub set_sender {
  291.     my $self = shift;
  292.  
  293.     $self->{sender} = shift;
  294. }
  295.  
  296. =item $msg->set_destination($name)
  297.  
  298. Set the name of the intended recipient of the message. This is
  299. typically used for signals to switch them from broadcast to
  300. unicast.
  301.  
  302. =cut
  303.  
  304. sub set_destination {
  305.     my $self = shift;
  306.     $self->{destination} = shift;
  307. }
  308.  
  309. =item my $iterator = $msg->iterator;
  310.  
  311. Retrieves an iterator which can be used for reading or
  312. writing fields of the message. The returned object is
  313. an instance of the C<Net::DBus::Binding::Iterator> class.
  314.  
  315. =cut
  316.  
  317. sub iterator {
  318.     my $self = shift;
  319.     my $append = @_ ? shift : 0;
  320.     
  321.     return Net::DBus::Test::MockIterator->_new(data => $self->{data},
  322.                            append => $append);
  323. }
  324.  
  325. =item $boolean = $msg->get_no_reply()
  326.  
  327. Gets the flag indicating whether the message is expecting
  328. a reply to be sent. 
  329.  
  330. =cut
  331.  
  332. sub get_no_reply {
  333.     my $self = shift;
  334.     
  335.     return $self->{no_reply};
  336. }
  337.  
  338. =item $msg->set_no_reply($boolean)
  339.  
  340. Toggles the flag indicating whether the message is expecting
  341. a reply to be sent. All method call messages expect a reply
  342. by default. By toggling this flag the communication latency
  343. is reduced by removing the need for the client to wait
  344.  
  345. =cut
  346.  
  347.  
  348. sub set_no_reply {
  349.     my $self = shift;
  350.     
  351.     $self->{no_reply} = shift;
  352. }
  353.  
  354. =item my @values = $msg->get_args_list
  355.  
  356. De-marshall all the values in the body of the message, using the 
  357. message signature to identify data types. The values are returned
  358. as a list.
  359.  
  360. =cut
  361.  
  362. sub get_args_list {
  363.     my $self = shift;
  364.     
  365.     my @ret;    
  366.     my $iter = $self->iterator;
  367.     if ($iter->get_arg_type() != &Net::DBus::Binding::Message::TYPE_INVALID) {
  368.     do {
  369.         push @ret, $iter->get();
  370.     } while ($iter->next);
  371.     }
  372.  
  373.     return @ret;
  374. }
  375.  
  376. =item $msg->append_args_list(@values)
  377.  
  378. Append a set of values to the body of the message. Values will
  379. be encoded as either a string, list or dictionary as appropriate
  380. to their Perl data type. For more specific data typing needs,
  381. the L<Net::DBus::Binding::Iterator> object should be used instead.
  382.  
  383. =cut
  384.  
  385. sub append_args_list {
  386.     my $self = shift;
  387.     my @args = @_;
  388.     
  389.     my $iter = $self->iterator(1);
  390.     foreach my $arg (@args) {
  391.     $iter->append($arg);
  392.     }
  393. }
  394.  
  395. =item my $sig = $msg->get_signature
  396.  
  397. Retrieves a string representing the type signature of the values
  398. packed into the body of the message.
  399.  
  400. =cut
  401.  
  402.  
  403. sub get_signature {
  404.     my $self = shift;
  405.  
  406.     my @bits = map { $self->_do_get_signature($_) } @{$self->{data}};
  407.     return join ("", @bits);
  408. }
  409.  
  410. sub _do_get_signature {
  411.     my $self = shift;
  412.     my $element = shift;
  413.  
  414.     if ($element->[0] == &Net::DBus::Binding::Message::TYPE_ARRAY) {
  415.     return chr(&Net::DBus::Binding::Message::TYPE_ARRAY) . $element->[2];
  416.     } elsif ($element->[0] == &Net::DBus::Binding::Message::TYPE_STRUCT) {
  417.     my @bits = map { $self->_do_get_signature($_) } @{$element->[1]};
  418.     return "{" . join("", @bits) . "}";
  419.     } elsif ($element->[0] == &Net::DBus::Binding::Message::TYPE_VARIANT) {
  420.     return chr(&Net::DBus::Binding::Message::TYPE_VARIANT);
  421.     } else {
  422.     return chr($element->[0]);
  423.     }
  424. }
  425.  
  426. 1;
  427.  
  428. =pod
  429.  
  430. =back
  431.  
  432. =head1 SEE ALSO
  433.  
  434. L<Net::DBus::Binding::Message>, L<Net::DBus::Test::MockConnection>, L<Net::DBus::Test::MockIterator>
  435.  
  436. =head1 AUTHOR
  437.  
  438. Daniel Berrange E<lt>dan@berrange.comE<gt>
  439.  
  440. =head1 COPYRIGHT
  441.  
  442. Copyright 2004 by Daniel Berrange
  443.  
  444. =cut
  445.